package com.jinlejia.feedback.question.adapter.question;

import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.jinlejia.feedback.R;
import com.jinlejia.feedback.databinding.FbAdapterQuestionDetailsTitleBinding;
import com.jinlejia.feedback.question.http.QuestionDetailsHelper;

import java.util.ArrayList;
import java.util.Map;

public class TitleViewHolderHelper {

    static void bindViewHolder(final KeyHelper.EventListener listener, RecyclerView.Adapter adapter, ViewHolder holder, ArrayList<Map<String, Object>> mData, final int position) {
        final FbAdapterQuestionDetailsTitleBinding binding = DataBindingUtil.getBinding(holder.itemView);
        final Map<String, Object> map = mData.get(position);
        binding.setMap(map);
        if (Boolean.parseBoolean(String.valueOf(map.get(KeyHelper.LocalIsShowAll)))){
            binding.simpleLayout.setVisibility(View.GONE);
            binding.infoLayout.setVisibility(View.VISIBLE);
            binding.showAllInfoTextView.setVisibility(View.GONE);
        }else {
            binding.simpleLayout.setVisibility(View.VISIBLE);
            binding.infoLayout.setVisibility(View.GONE);
            binding.showAllInfoTextView.setVisibility(View.VISIBLE);
        }

        if (binding.showAllInfoTextView.getVisibility()==View.GONE){
            binding.infoLayout.setVisibility(View.VISIBLE);
            binding.simpleLayout.setVisibility(View.GONE);
        }else {
            binding.infoLayout.setVisibility(View.GONE);
            binding.simpleLayout.setVisibility(View.VISIBLE);
        }

        //region 收藏操作
        final String collectStateKey = "collectState";
        final String collectCountKey = "collectCount";
        boolean isCollect = Boolean.parseBoolean(String.valueOf(map.get(collectStateKey)));//是否已收藏
        int collectCount;
        try{
            collectCount = Integer.parseInt(String.valueOf(map.get(collectCountKey)));//收藏数量
        }catch (Exception e){
            collectCount = 0;
        }
        binding.collectCountTextView.setText(String.valueOf(collectCount));
        binding.simpleCollectCountTextView.setText(String.valueOf(collectCount));
        if (isCollect){
            binding.simpleCollectStateImageView.setImageResource(R.mipmap.fb_collect_icon);
            binding.simpleCollectCountTextView.setTextColor(Color.parseColor("#FCCB00"));

            binding.collectStateImageView.setImageResource(R.mipmap.fb_collect_icon);
            binding.collectCountTextView.setTextColor(Color.parseColor("#FCCB00"));
        }else {
            binding.simpleCollectStateImageView.setImageResource(R.mipmap.fb_uncollect_icon);
            binding.simpleCollectCountTextView.setTextColor(Color.parseColor("#999999"));

            binding.collectStateImageView.setImageResource(R.mipmap.fb_uncollect_icon);
            binding.collectCountTextView.setTextColor(Color.parseColor("#999999"));
        }

        View.OnClickListener collectListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //region 本地收藏数量增加
                boolean isCollect = Boolean.parseBoolean(String.valueOf(map.get(collectStateKey)));//是否已收藏
                /*
                int count = Integer.parseInt(binding.simpleCollectCountTextView.getText().toString());
                if (isCollect){ //如果已经收藏,执行取消收藏操作，收藏数量减少1
                    binding.simpleCollectStateImageView.setImageResource(R.mipmap.fb_uncollect_icon);
                    binding.simpleCollectCountTextView.setTextColor(Color.parseColor("#999999"));

                    binding.collectStateImageView.setImageResource(R.mipmap.fb_uncollect_icon);
                    binding.collectCountTextView.setTextColor(Color.parseColor("#999999"));
                    count = count - 1;
                }else { //如果未收藏，执行收藏操作，收藏数量加1
                    binding.simpleCollectStateImageView.setImageResource(R.mipmap.fb_collect_icon);
                    binding.simpleCollectCountTextView.setTextColor(Color.parseColor("#FCCB00"));

                    binding.collectStateImageView.setImageResource(R.mipmap.fb_collect_icon);
                    binding.collectCountTextView.setTextColor(Color.parseColor("#FCCB00"));
                    count = count + 1;
                }
                binding.simpleCollectCountTextView.setText(String.valueOf(count));
                binding.collectCountTextView.setText(String.valueOf(count));
                map.put(collectStateKey,!isCollect);
                map.put(collectCountKey,count);
                */
                //endregion
                listener.onCollectOperation(isCollect,String.valueOf(map.get("problemCode")), QuestionDetailsHelper.LikeType.Question,map,position);

            }
        };
        binding.simpleCollectOperateLayout.setOnClickListener(collectListener);
        binding.collectOperateLayout.setOnClickListener(collectListener);
        //endregion

        //region 点赞操作

        final String likeStateKey = "likeState";
        final String likeCountKey = "likeCount";
        int likeCount;
        try{
            likeCount = Integer.parseInt(String.valueOf(map.get(likeCountKey)));//点赞数量
        }catch (Exception e){
            likeCount = 0;
        }
        boolean isLike = (boolean) map.get(likeStateKey);
        binding.likeCountTextView.setText(String.valueOf(likeCount));
        binding.simpleLikeCountTextView.setText(String.valueOf(likeCount));
        if (isLike){
            binding.simpleLikeStateImageView.setImageResource(R.mipmap.fb_like_icon);
            binding.simpleLikeCountTextView.setTextColor(Color.parseColor("#FCCB00"));

            binding.likeStateImageView.setImageResource(R.mipmap.fb_like_icon);
            binding.likeCountTextView.setTextColor(Color.parseColor("#FCCB00"));
        }else {
            binding.simpleLikeStateImageView.setImageResource(R.mipmap.fb_unlike_icon);
            binding.simpleLikeCountTextView.setTextColor(Color.parseColor("#999999"));

            binding.likeStateImageView.setImageResource(R.mipmap.fb_unlike_icon);
            binding.likeCountTextView.setTextColor(Color.parseColor("#999999"));
        }

        View.OnClickListener likeListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = Integer.parseInt(binding.simpleLikeCountTextView.getText().toString());
                boolean isLike = (boolean) map.get(likeStateKey);
                if (isLike){
                    binding.simpleLikeStateImageView.setImageResource(R.mipmap.fb_unlike_icon);
                    binding.simpleLikeCountTextView.setTextColor(Color.parseColor("#999999"));

                    binding.likeStateImageView.setImageResource(R.mipmap.fb_unlike_icon);
                    binding.likeCountTextView.setTextColor(Color.parseColor("#999999"));
                    count = count - 1;
                }else {
                    binding.simpleLikeStateImageView.setImageResource(R.mipmap.fb_like_icon);
                    binding.simpleLikeCountTextView.setTextColor(Color.parseColor("#FCCB00"));

                    binding.likeStateImageView.setImageResource(R.mipmap.fb_like_icon);
                    binding.likeCountTextView.setTextColor(Color.parseColor("#FCCB00"));
                    count = count + 1;
                }
                listener.onLikeOperation(isLike,String.valueOf(map.get("problemCode")), QuestionDetailsHelper.LikeType.Question,map,position);
                binding.simpleLikeCountTextView.setText(String.valueOf(count));
                binding.likeCountTextView.setText(String.valueOf(count));
                map.put(likeStateKey,!isLike);
                map.put(likeCountKey,count);
            }
        };

        binding.simpleLikeOperateLayout.setOnClickListener(likeListener);
        binding.likeOperateLayout.setOnClickListener(likeListener);
        //endregion

        //region 用户信息
        KeyHelper.showUserInfo(binding.userInfo,map.get("askUserName"),map.get("askUserPortrait"),
                map.get("askUserDept"),map.get("askUserDuty")
        );
        //endregion

        binding.showAllInfoTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.simpleLayout.setVisibility(View.GONE);
                binding.infoLayout.setVisibility(View.VISIBLE);
                binding.showAllInfoTextView.setVisibility(View.GONE);
                map.put(KeyHelper.LocalIsShowAll,true);
            }
        });
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
}
